package com.example.chinalin.viewpagerdemo;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.chinalin.viewpagerdemo.viewpager.MyJazzyViewPager;

public class MainActivity extends Activity {

    protected static final String TAG = "MainActivity";
    private int[] mImgIds;
    private MyJazzyViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImgIds = new int[] { R.drawable.image_1, R.drawable.image_2, R.drawable.image_3,
                R.drawable.image_4 };
        mViewPager = (MyJazzyViewPager) findViewById(R.id.id_viewPager);
/*        try {
            Field mField =OldViewPager.class.getDeclaredField("mScroller");
            mField.setAccessible(true);
            FixedSpeedScroller mScroller = new FixedSpeedScroller(MainActivity.this, new AccelerateInterpolator());
            mScroller.setmDuration(1500);    //在这里设置时间单位毫秒
            mField.set(mViewPager, mScroller); //viewPager和FixedSpeedScrolle
        }catch(Exception e){
            e.printStackTrace();
        }*/
        mViewPager.setAdapter(new PagerAdapter()
        {

            @Override
            public boolean isViewFromObject(View arg0, Object arg1)
            {
                return arg0 == arg1;
            }

            @Override
            public void destroyItem(ViewGroup container, int position,
                                    Object object)
            {
                container.removeView((View) object);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position)
            {
                RelativeLayout relativeLayout = new RelativeLayout(MainActivity.this);
                setupRelayout(relativeLayout,position);
                container.addView(relativeLayout);
                mViewPager.setObjectForPosition(relativeLayout, position);
                return relativeLayout;
            }

            @Override
            public int getCount()
            {
                return mImgIds.length;
            }
        });

    }
    private void setupRelayout(RelativeLayout relativeLayout, int position){
        addImageView(relativeLayout,position);
        addButton(relativeLayout,position);
    }
    private  void addButton(RelativeLayout relativeLayout,int position){
                Button button = new Button(this) ;

//                RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(300,300);
//                lp.setMargins(i*300,j*300, 0, 0);
//                button.setLayoutParams(lp);
//                button.requestLayout();
                button.setText("button " + position);
                relativeLayout.addView(button);
    }

    private  void addImageView(RelativeLayout relativeLayout, int position){
        ImageView imageView = new ImageView(MainActivity.this);
        imageView.setImageResource(mImgIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
        relativeLayout.addView(imageView);
    }

  /*  public class FixedSpeedScroller extends Scroller {
        private int mDuration = 1500;
        public FixedSpeedScroller(Context context) {
            super(context);
        }
        public FixedSpeedScroller(Context context, Interpolator interpolator) {
            super(context, interpolator);
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy, int duration) {
            // Ignore received duration, use fixed one instead
            super.startScroll(startX, startY, dx, dy, mDuration);
        }
        @Override
        public void startScroll(int startX, int startY, int dx, int dy) {
            // Ignore received duration, use fixed one instead
            super.startScroll(startX, startY, dx, dy, mDuration);
        }
        public void setmDuration(int time){
            mDuration = time;
        }
        public int getmDuration(){
            return mDuration;
        }

    }*/
}
